package com.huixh.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.business.domain.TrainStation;
import com.huixh.train.business.domain.TrainStationExample;
import com.huixh.train.business.mapper.TrainStationMapper;
import com.huixh.train.business.req.TrainStationQueryReq;
import com.huixh.train.business.req.TrainStationSaveReq;
import com.huixh.train.business.resp.TrainStationQueryResp;
import com.huixh.train.business.service.TrainStationService;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class TrainStationServiceImpl implements TrainStationService {

    private final Logger logger = LoggerFactory.getLogger(TrainStationServiceImpl.class);
    @Resource
    private TrainStationMapper trainStationMapper;

    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(TrainStationSaveReq trainStationSaveReq) {
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(trainStationSaveReq, TrainStation.class);
        if (ObjectUtil.isNull(trainStation.getId())) {
            trainStation.setId(snowUtil.getSnowflakeToLong());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKey(trainStation);
        }
    }

    @Override
    public PageResult<List<TrainStationQueryResp>> queryList(TrainStationQueryReq trainStationQueryReq) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("train_code asc, `index` asc");
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        if (StrUtil.isNotBlank(trainStationQueryReq.getTrainCode())) {
            criteria.andTrainCodeEqualTo(trainStationQueryReq.getTrainCode());
        }
        logger.info("查询页码：{}", trainStationQueryReq.getPageNum());
        logger.info("每页条数：{}", trainStationQueryReq.getPageSize());
        PageHelper.startPage(trainStationQueryReq.getPageNum(), trainStationQueryReq.getPageSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(trainStationExample);

        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        logger.info("总行数：{}", pageInfo.getTotal());
        logger.info("总页数：{}", pageInfo.getPages());

        List<TrainStationQueryResp> list = BeanUtil.copyToList(trainStationList, TrainStationQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), list);
    }

    @Override
    public void delete(Long id) {
        trainStationMapper.deleteByPrimaryKey(id);
    }

    private TrainStation selectByUnique(String trainCode, Integer index) {
        TrainStationExample carriageExample = new TrainStationExample();
        carriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andIndexEqualTo(index);
        List<TrainStation> trainStations = trainStationMapper.selectByExample(carriageExample);

        if (CollUtil.isEmpty(trainStations)) {
            return null;
        } else {
            return trainStations.get(0);
        }
    }

    private TrainStation selectByUnique(String trainCode, String name) {
        TrainStationExample carriageExample = new TrainStationExample();
        carriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andNameEqualTo(name);
        List<TrainStation> trainStations = trainStationMapper.selectByExample(carriageExample);

        if (CollUtil.isEmpty(trainStations)) {
            return null;
        } else {
            return trainStations.get(0);
        }
    }

    /**
     *
     * @param trainCode 火车车次代码，用于查询特定车次的车站信息。
     * @return
     */
    @Override
    public List<TrainStation>  selectByTrainCode(String trainCode) {
        TrainStationExample carriageExample = new TrainStationExample();
        carriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(carriageExample);
    }
}